package controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import common.EasyuiHelper;
import common.JsonTreeData;
import Dao.JCXX.*;
import Dao.SYS.*;
import Model.JCXX.*;
import Model.SYS.*;

@Controller
public class SysController {

	@Autowired
	private SysManageMapper sysManageMapper;

	@RequestMapping(value = "/sys/selectModuleMap", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectModuleMap(HttpServletRequest request) {
		try {
			String userid = (String) request.getSession().getAttribute(
					"display_userid");
			List<HashMap<String, Object>> list = sysManageMapper
					.selectModuleMap(userid);
			List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
			for (Map<String, Object> map : list) {
				System.out.println("moduleid" + map.get("MODULEID"));
				JsonTreeData treeData = new JsonTreeData();
				treeData.setId(map.get("MODULEID").toString());
				treeData.setPid(map.get("PARENTMODULEID").toString());
				treeData.setText(map.get("MODULENAME").toString());
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", map.get("MODULEURL").toString());
				treeData.setAttributes(attributes);
				treeDataList.add(treeData);

			}
			List<JsonTreeData> newTreeDataList = EasyuiHelper.getTreeJson(
					treeDataList, "0");

			JSONArray jsonarray = JSONArray.fromObject(newTreeDataList);
			String slist = jsonarray.toString();
			System.out.println(slist);
			//request.setAttribute("addLists", list);
			return slist;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "" + e.getMessage());
			System.out.println(e.getMessage());
			return "test/test";
		}
	}

}
